package gr.handmademarket.ui.views;

import static gr.handmademarket.util.CommonStatics.PRODUCTS_PICS_PATH;
import gr.handmademarket.po.implementations.Product;
import gr.handmademarket.ui.links.CategoryLink;
import gr.handmademarket.ui.pages.ProductDetailsPage;
import gr.handmademarket.ui.panels.SellerProductsPanel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.ContextRelativeResource;

/**
 * images grid view.
 */
public class ProductsInfoListGrid 
extends GridView<Product>{

	/**
	 * category or seller indicator.
	 */
	private boolean localCategoryIndication;
	
	private SellerProductsPanel caller;

	/**
	 * Creates a new ProductsListGrid object. 
	 *
	 * @param id
	 * @param dataProvider
	 * @param isCategory 
	 */
	public ProductsInfoListGrid(String id, IDataProvider<Product> dataProvider, boolean isCategory, SellerProductsPanel caller) {
		super(id, dataProvider);
		this.caller = caller;
		localCategoryIndication = isCategory;
	}

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void populateEmptyItem(Item<Product> item) {
		// empty
	}

	@SuppressWarnings("nls")
	@Override
	protected void populateItem(Item<Product> item) {
		Product product = item.getModelObject();
		AjaxLink<NonCachingImage> link = createAjaxLink(product);
		item.add(link);
		Label nameLabel = new Label("name", product.getName());
		item.add(nameLabel);
		CategoryLink categoryLink = new CategoryLink("referenceLink");
		item.add(categoryLink);
		if (localCategoryIndication){
			item.add(new Label("prefix","created by"));
			categoryLink.add(new Label("reference",product.getSeller().getName()));
			categoryLink.setCategory(product.getSeller());
		} else{
			item.add(new Label("prefix","belongs to "));
			categoryLink.add(new Label("reference",product.getCategory().getName()));
			categoryLink.setCategory(product.getCategory());			
		}
	}
	
	/**
	 * @param product
	 * @return ajax link with the product thumbnail leading to product detail page.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<NonCachingImage> createAjaxLink(Product product){
		final Product p = product;
		String path = PRODUCTS_PICS_PATH + p.getCode() + ".jpg"; 
		ContextRelativeResource resource = new ContextRelativeResource(path);
		NonCachingImage image = new NonCachingImage("img", resource);
		AjaxLink<NonCachingImage> ajaxLink = new AjaxLink<NonCachingImage>("link", new Model<NonCachingImage>(image)) {
			/**
			 * serialVersionUID
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				if (caller != null){
					caller.callback(target, p);
				} else {
					ProductDetailsPage page = new ProductDetailsPage(new CompoundPropertyModel<Product>(p));
					setResponsePage(page);
				}
			}
		};
		ajaxLink.add(image);
		return ajaxLink;
	}

}
